<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  	<el-breadcrumb-item :to="{ path: '/goods' }">首页</el-breadcrumb-item>
		  	<el-breadcrumb-item>添加属性</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form ref="form" :model="attr" label-width="80px">
		  	<el-form-item label="属性名称">
		    	<el-input v-model="attr.attr_name"></el-input>
		  	</el-form-item>

		  	<el-form-item label="选择类目">
			    <el-select v-model="attr.cate_id" @change="isIcon" placeholder="请选择数据库">
			      	<el-option  v-for="(item,index) in cateList" :key="item.id" :label="item.cate_name" :value="index"></el-option>
			    </el-select>
			</el-form-item>

			<el-form-item v-if="isImg" label="属性图标">
				<el-upload
				  class="upload-demo"
				  action="/api/goods/upload"
				  :on-preview="handlePreview"
				  :on-remove="handleRemove"
				  :file-list="detailPic"
				  :on-success="successEvent"
				  list-type="picture">
				  <el-button size="small" type="primary">点击上传</el-button>
				  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
				</el-upload>
		  	</el-form-item>

		  	<el-form-item label="是否启用">
		    	<el-switch v-model="attr.status"></el-switch>
		  	</el-form-item>

		  	<el-form-item>
		    	<el-button type="primary" @click="addAttr">添加属性</el-button>
		    	<el-button>取消</el-button>
		  	</el-form-item>
		</el-form>
	</div>
</template>

<script type="text/javascript">
	export default{
		data(){
			return{
				token:"",
				attr:{
					attr_name:"",
					status:true,
					cate_id:null,
					headPic:""
				},
				cateList:[],
				isImg:false,
				detailPic:[]

			}
		},
		created(){
			this.token = window.axios.defaults.headers.common['X-CSRF-TOKEN'];
			this.getCateList();
		},
		methods:{
			handleRemove(file, fileList) {
		        console.log(file, fileList);
		    },
		    handlePreview(file) {
		        console.log(file);
		   	},
		    handlePictureCardPreview(file) {
		        this.dialogImageUrl = file.url;
		        this.dialogVisible = true;
		    },
		    handleExceed(files, fileList) {
	        	this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
	      	},
		    beforeRemove(file, fileList) {
		        return this.$confirm(`确定移除 ${ file.name }？`);
		    },
		    addAttr(){
		    	this.attr.cate_id = this.cateList[this.attr.cate_id].id;
		    	axios.post('/api/attrs/add',{_token:this.token,attr_name:this.attr.attr_name,status:this.attr.status,cate_id:this.attr.cate_id,headPic:this.attr.headPic})
		    		.then(result => {
		    			if(result.data.code == 200){
		    				this.$notify({
		    					type:"success",
		    					title:"成功添加",
		    					message:result.data.message
		    				})
		    				this.attr.attr_name = ""
		    			}else{
		    				this.$notify.error(result.data.message);
		    			}
		    		})
		    		.catch(error => {

		    		})
		    },
		    getCateList()
		    {
		    	axios.get('/api/cates')
					.then(result => {
						console.log(result);
						if(result.data.code == 200){
							this.cateList = result.data.result;
						}
					})
					.catch(error => {

					})
		    },
		    isIcon(item)
		    {
		    	if(this.cateList[item].type == 3){
		    		this.isImg = true;
		    	}else{
		    		this.isImg = false;
		    	}
		    },
		    successEvent(file){
		    	this.attr.headPic = file.result;
		    },
		    handleRemove(){

		    },
		    handlePreview(){

		    }
		}
	}
</script>

<style type="text/css">
	input[type=file] {
	    display: none;
	}
</style>